<?php 
/* ================================================================================
 * LWAdmin	 
 *
 * users.php: This is the users file for Living Web.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 2/18/05
   ================================================================================ */
	
	global $SSO, $Security, $AppID, $User, $DB;

	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
	
	//Check to see if the user has rights to be on LWAdmin
	$PageRights = array('ADMIN', 'USERADMIN');
	$Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);
	
	function GenerateUsersOptions()
	{
		global $DB;
		$Options = "";
		$DB->query("SELECT * FROM users where username not like 'AUTO%' ORDER BY LastName");
		$row = $DB->fetch_row();
		$Options = $Options . '<option selected value="' . $row['Username'] . '">' . $row['LastName'] . ', ' . $row['FirstName'] . '  ' . $row['MiddleName'] . ' (' . $row['Username'] . ')' . '</option>';
		while($row = $DB->fetch_row())
		{	
			$Options = $Options . '<option value="' . $row['Username'] . '">' . $row['LastName'] . ', ' . $row['FirstName'] . '  ' . $row['MiddleName'] . ' (' . $row['Username'] . ')' . '</option>';
		}
		
		return $Options;
	}
	
	if(isset($_POST['ResetPass']) && isset($_POST['selUsers']))
	{
		global $DB;
		$DB->query("select * from users where Username = '" . $_POST['selUsers'] . "'"); 
		$row = $DB->fetch_row();
		
		$new_temp_password = str_shuffle($row['LastName'] . $row['FirstName']);
		$email = $row['Email'];
		
		$DB->query("update users set PasswordTemp = '" . $new_temp_password ."' where Username = '" . $_POST['selUsers'] . "'");
		$DB->query("update users set Password = '' where Username = '" . $_POST['selUsers'] . "'");
		
		if($row['Email'] != "")
		{
			if(mail($email ,"Website Account Creation","Dear " . $row['FirstName'] . " " . $row['LastName'] . ",\r\n\r\n Your account has been created. Your username is " . $row['Username'] . "\r\n\r\n Your temporary password is \r\n\r\n           " . $new_temp_password . " \r\n\r\nIf you have any questions, then please email admin@lwbc.net.","From: Living Word Baptist Church <admin@lwbc.net>" . "\r\n" . "Reply-To: admin@lwbc.net"))
			{
				print '<br><br><span class="T2" style="color:#FF0000;">Email Sent</span><br><br>';
			}
			else
			{
				print '<br><br><span class="T2" style="color:#FF0000;">Email NOT Sent</span><br><br>';
			}
		}
		else
		{
			print "<script>alert('Person doesn\'t have an email!');</script>";
		}
	}
?>


<table style="border-spacing:0px;border:0px;padding:0px;" >
	<tr>
		<td>
			<form action="index.php?action=users" method="post" name="frmUsers">
			<table style="border-spacing:0px;border:0px;padding:5px;width:95%;">
				<tr>
					<td>
						<span class="TB1" style="color:#555555;">&nbsp;Users List</span>
					</td>
				</tr>
				<tr>
					<td valign="top"  align="center">
						<table cellpadding="5" cellspacing="0">
							<tr>
								<td align="center">
									<a href="#" class="L1" onClick="location.href='index.php?action=users_new';">New</a>
								</td>
								<td>|</td>
								<td align="center">
									<a href="#" class="L1" onClick="frmUsers.action='index.php?action=users_edit';frmUsers.submit();">Edit</a>
								</td>
								<td>|</td>
								<td align="center">
									<a href="#" class="L1" onClick="frmUsers.action='index.php?action=users_delete';frmUsers.submit();">Delete</a>
								</td>
								<td>|</td>
								<td align="center">
									<a href="#" class="L1" onClick="frmUsers.action='index.php?action=users_access';frmUsers.submit();">Access</a>
								</td>
								<td>|</td>
								<td align="center">
									<a href="#" class="L1" onClick="frmUsers.action='index.php?action=users_ministries';frmUsers.submit();">Ministry</a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td valign="middle"  align="center">
						<select name="selUsers"  class="LivingWebText" multiple size="10">
							<?php print GenerateUsersOptions(); ?>
						</select> 
					</td>
				</tr>
				<tr>
					<td align="center"><span class="T1" style="font-size:10px;">Select Only One</span>
					</td>
				</tr>
				<tr>
					<td align="center"><br><br><input type="submit" value="Reset Temporary Password" class="B" name="ResetPass" onClick="if(confirm('Are you sure you would like to reset this person\'s password?')){return true;}else{return false;}"></td>
				</tr>
			</table>
			</form>
		</td>
	</tr>
</table>
